//
//  ControllerForClickButton.m
//  ProjectA
//
//  Created by dllo on 16/2/24.
//  Copyright © 2016年 刘旺辉. All rights reserved.
//

#import "ViewControllerForClickButton.h"    //C
#import "ControllerForClickItem.h"

#import "CellForClickButton.h"  //V

#import "ModelForSection1.h"

@interface ViewControllerForClickButton ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *arrForData;

@end

@implementation ViewControllerForClickButton
- (void)dealloc {
    
    self.collectionView.dataSource = nil;
    self.collectionView.delegate = nil;
    
    [_collectionView release];
    [_arrForData release];
    [super dealloc];
}
- (void)setLightDayModel {
    [super setLightDayModel];
    self.barBackgroundView.backgroundColor = RGBA(44, 211, 209, 1);
}

- (void)setNightDayModel {
    [super setNightDayModel];
    self.barBackgroundView.backgroundColor = RGBA(52, 52, 52, 1);
}
- (void)viewWillAppear:(BOOL)animated {
    if (ISNIGHT) {
        [self setNightDayModel];
    } else {
        [self setLightDayModel];
    }
}
- (void)viewWillDisappear:(BOOL)animated {
    self.barBackgroundView.backgroundColor = L_COLOR_CLEAR;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 加载数据菊花
    [self.view addSubview:[UIViewForActivityIndicator shareActivityIndicatorViewWithFrame:self.view.frame]];
    
    [self handleData];
    [self settingsNavigationController];
    [self createCollectionView];
    
}

- (void)handleData {
    self.arrForData = [NSMutableArray array];
    [NetWorkHandler networkingWithUrl:@"http://api.breadtrip.com/v2/new_trip/spot/hot/list/?start=0" comleteHandle:^(id result, NSData *data) {
        
        NSDictionary *dic = [result objectForKey:@"data"];
        NSArray *arr = [dic objectForKey:@"hot_spot_list"];
        for (NSDictionary *dic in arr) {
            
            ModelForSection1 *model = [[ModelForSection1 alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            NSDictionary *user = [dic objectForKey:@"user"];
            [model setValuesForKeysWithDictionary:user];
            [self.arrForData addObject:model];
            [model release];
            
        }
        [self.collectionView reloadData];
        [[UIViewForActivityIndicator shareActivityIndicatorViewWithFrame:self.view.frame] didFinishLoad];
    }];
    
}

- (void)createCollectionView {
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(([UIScreen mainScreen].bounds.size.width - 10 - 10 - 10) / 2.f, 235);
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.minimumLineSpacing = 10;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor clearColor];
    [flowLayout release];
    [self.view addSubview:self.collectionView];
    [_collectionView release];
    
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    
    [self.collectionView registerClass:[CellForClickButton class] forCellWithReuseIdentifier:@"pool"];
    
    // 上拉加載
    self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        static int number = 0;
        number = number + 12;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            NSString *str = [NSString stringWithFormat:@"http://api.breadtrip.com/v2/new_trip/spot/hot/list/?start=%@", @(number)];
            [NetWorkHandler networkingWithUrl:str comleteHandle:^(id result, NSData *data) {
                
                NSDictionary *dic = [result objectForKey:@"data"];
                NSArray *arr = [dic objectForKey:@"hot_spot_list"];
                for (NSDictionary *dic in arr) {
                    
                    ModelForSection1 *model = [[ModelForSection1 alloc] init];
                    [model setValuesForKeysWithDictionary:dic];
                    NSDictionary *user = [dic objectForKey:@"user"];
                    [model setValuesForKeysWithDictionary:user];
                    [self.arrForData addObject:model];
                    [model release];
                    
                }
                [self.collectionView reloadData];
                [self.collectionView.mj_footer endRefreshing];
                
            }];
        });
    }];
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.arrForData.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CellForClickButton *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool" forIndexPath:indexPath];
    [cell passModel:[self.arrForData objectAtIndex:indexPath.item]];
    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    NSLog(@"%ld", indexPath.item);
    ControllerForClickItem *item = [[ControllerForClickItem alloc] init];
//    item.model
    ModelForSection1 *model = [self.arrForData objectAtIndex:indexPath.item];
    NSString *spot_id = [NSString stringWithFormat:@"http://api.breadtrip.com/v2/new_trip/spot/?spot_id=%@", model.spot_id];
    item.model = model;
    item.spot_id = spot_id;
    [self.navigationController pushViewController:item animated:YES];
    [item release];
}


- (void)settingsNavigationController {
    self.navigationItem.title = @"精选故事";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-fanhui(heise)"] style:UIBarButtonItemStylePlain target:self action:@selector(houtuiAction:)];
    
}

- (void)houtuiAction:(UIBarButtonItem *)button {
    
    [self.navigationController popViewControllerAnimated:YES];
    self.tabBarController.tabBar.hidden = NO;
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
